# Bitstring syntax extension.
# Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
#
# $Id: Makefile.in 160 2008-08-27 11:27:47Z richard.wm.jones $

PACKAGE		= @PACKAGE_NAME@
VERSION		= @PACKAGE_VERSION@

OCAMLFIND	= @OCAMLFIND@
OCAMLMKLIB	= @OCAMLMKLIB@
OCAMLDOC	= @OCAMLDOC@
INSTALL		= @INSTALL@
TIME		= @TIME@
GPROF		= @GPROF@
BISECT_REPORT	= @BISECT_REPORT@

BYTESWAP_H	= @BYTESWAP_H@

OCAMLLIB	= @OCAMLLIB@
top_srcdir	= @top_srcdir@

pkg_cil		= @pkg_cil@
pkg_extlib	= @pkg_extlib@

enable_coverage = @enable_coverage@

OCAMLCFLAGS	= -g
OCAMLCPACKAGES	=
OCAMLCLIBS	= -linkpkg
OCAMLOPTFLAGS	=
OCAMLOPTPACKAGES =
OCAMLOPTLIBS	= -linkpkg

ifneq ($(enable_coverage),no)
OCAMLCFLAGS     += -I +bisect -pp 'camlp4o $(OCAMLLIB)/bisect/instrument.cma'
OCAMLCLIBS	+= -I +bisect bisect.cma
OCAMLOPTFLAGS   += -I +bisect -pp 'camlp4o $(OCAMLLIB)/bisect/instrument.cma'
OCAMLOPTLIBS	+= -I +bisect bisect.cmxa
endif

OCAMLDOCFLAGS	= -html -sort

CC		= @CC@
CFLAGS		= @CFLAGS@ -Wall -Werror -fPIC -I$(top_srcdir) -I$(OCAMLLIB)

LIBRARY_PATH	= @abs_top_builddir@
LD_LIBRARY_PATH	= @abs_top_builddir@
export LIBRARY_PATH
export LD_LIBRARY_PATH

SUBDIRS		=
ifneq ($(pkg_cil),no)
ifneq ($(pkg_extlib),no)
SUBDIRS		+= cil-tools
endif
endif

EXAMPLES	= $(patsubst %.ml,%,$(wildcard examples/*.ml))

TESTS		= $(patsubst %.ml,%,$(wildcard tests/*.ml))

BENCHMARKS	= $(patsubst %.ml,%,$(wildcard benchmarks/*.ml))

all:	$(BYTESWAP_H) \
	bitstring.cma bitstring_persistent.cma \
	bitstring.cmxa bitstring_persistent.cmxa \
	pa_bitstring.cmo \
	bitstring-objinfo
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

bitstring.cma: bitstring_types.cmo bitstring_config.cmo \
	  bitstring_c.o bitstring.cmo bitmatch.cmo
	$(OCAMLMKLIB) -o bitstring $^

bitstring_persistent.cma: bitstring_persistent.cmo
	$(OCAMLFIND) ocamlc -a -o $@ $^

bitstring_persistent.cmo: bitstring_persistent.ml
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) \
	  -I +camlp4 -pp camlp4of -c $<

bitstring.cmxa: bitstring_types.cmx bitstring_config.cmx \
	  bitstring_c.o bitstring.cmx bitmatch.cmx
	$(OCAMLMKLIB) -o bitstring $^

bitstring_persistent.cmxa: bitstring_persistent.cmx
	$(OCAMLFIND) ocamlopt -a -o $@ $^

bitstring_persistent.cmx: bitstring_persistent.ml
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTPACKAGES) \
	  -I +camlp4 -pp camlp4of -c $<

bitstring_persistent.cmi: bitstring_persistent.mli
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) \
	  -I +camlp4 -pp camlp4of -c $<

pa_bitstring.cmo: pa_bitstring.ml bitstring.cma bitstring_persistent.cma
	$(OCAMLFIND) ocamlc bitstring.cma -I +camlp4 camlp4lib.cma \
	  -pp camlp4of -c $< -o $@

bitstring-objinfo: bitstring_objinfo.cmo bitstring.cma bitstring_persistent.cma
	$(OCAMLFIND) ocamlc -I +camlp4 unix.cma camlp4lib.cma \
	  $(OCAMLCLIBS) \
	  bitstring.cma bitstring_persistent.cma \
	  $< -o $@

# Create byteswap.h if the system doesn't have this file.
# From gnulib, license is LGPLv2+.

byteswap.h: byteswap.in.h
	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
	  cat $(srcdir)/byteswap.in.h; \
	} > $@-t
	mv -f $@-t $@

# Tests.

ifeq ($(enable_coverage),no)
PP	= -pp 'camlp4o bitstring.cma bitstring_persistent.cma pa_bitstring.cmo'
else
PP	= -pp 'camlp4o $(OCAMLLIB)/bisect/bisect.cma bitstring.cma bitstring_persistent.cma pa_bitstring.cmo'
endif

test: pa_bitstring.cmo \
	bitstring.cma bitstring_persistent.cma \
	bitstring.cmxa bitstring_persistent.cmxa \
	tests/test.bmpp
	@for f in $(TESTS); do \
	  echo Building $$f; \
	  $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(PP) -package unix $(OCAMLOPTLIBS) -I . bitstring.cmxa $$f.ml -o $$f.opt; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	  echo Running $$f; \
	  $$f.opt; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	done
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

tests/test.bmpp: create_test_pattern
	./$< $@.new
	mv $@.new $@

create_test_pattern: create_test_pattern.cmo
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -pp camlp4of \
	  unix.cma -I +camlp4 camlp4lib.cma \
	  $(OCAMLCLIBS) \
	  -I . bitstring.cma bitstring_persistent.cma $< -o $@

create_test_pattern.cmo: create_test_pattern.ml
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) -pp camlp4of \
	  unix.cma -I +camlp4 \
	  -I . -c $< -o $@

# Coverage of tests.

ifneq ($(BISECT_REPORT),no)
coverage:
	rm -f coverage-report/*
	rm -f bisect*.out
	$(MAKE) test
	$(BISECT_REPORT) -html coverage-report bisect*.out
	rm -f bisect*.out
	@echo Coverage report is in `pwd`/coverage-report/index.html
endif

# Examples.

examples: pa_bitstring.cmo bitstring.cma bitstring_persistent.cma
	@for f in $(EXAMPLES); do \
	  echo Building $$f; \
	  $(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(PP) \
	    -package unix -linkpkg -I . bitstring.cma $$f.ml -o $$f; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	done
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

print-tests: pa_bitstring.cmo bitstring_persistent.cma
	@for f in $(TESTS); do \
	  echo Test: $$f.ml; \
	  cmd="camlp4o bitstring.cma bitstring_persistent.cma pa_bitstring.cmo \
	    -printer pr_o.cmo $$f.ml"; \
	  echo $$cmd; \
	  $$cmd; \
	done

print-examples: pa_bitstring.cmo
	@for f in $(EXAMPLES); do \
	  echo Example: $$f.ml; \
	  camlp4o bitstring.cma bitstring_persistent.cma pa_bitstring.cmo \
	    -printer pr_o.cmo $$f.ml; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	done

# Benchmarks.

ifneq ($(TIME),no)

benchmarks: pa_bitstring.cmo bitstring.cma bitstring_persistent.cma
	@for f in $(BENCHMARKS); do \
	  $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(PP) \
	    -package unix -linkpkg -I . bitstring.cmxa $$f.ml -o $$f.opt; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	  $(TIME) -f "$$f: %e seconds" $$f.opt; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	done
ifneq ($(GPROF),no)
	@for f in $(BENCHMARKS); do \
	  $(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(PP) -p \
	    -package unix -linkpkg -I . bitstring.cmxa $$f.ml -o $$f.opt.p; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	  rm -f gmon.out; \
	  $$f.opt.p; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	  gprof $$f.opt.p > $$f.profile; \
	  if [ $$? -ne 0 ]; then exit 1; fi; \
	  echo "Wrote profile of $$f"; \
	  echo "    to $$f.profile"; \
	  rm -f gmon.out; \
	done
endif

endif

# Clean.

clean:
	rm -f core *~ *.cmi *.cmo *.cmx *.cma *.cmxa *.cmp *.a *.o *.so
	rm -f tests/*~ tests/*.cmi tests/*.cmo tests/*.cmx tests/*.opt
	rm -f tests/*.o
	rm -f examples/*~ examples/*.cmi examples/*.cmo examples/*.cmx
	rm -f examples/*.o $(EXAMPLES)
	rm -f benchmarks/*~ benchmarks/*.cmi benchmarks/*.cmo
	rm -f benchmarks/*.cmx benchmarks/*.o
	rm -f benchmarks/*.opt benchmarks/*.opt.p benchmarks/*.profile
	rm -f create_test_pattern bitstring-objinfo
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

distclean: clean
	rm -rf autom4te.cache
	rm -f config.log config.status
	rm -f config.h Makefile META bitstring_config.ml
	rm -f bitstring-objinfo
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

# Documentation.

ifneq ($(OCAMLDOC),)
doc:
	rm -rf html
	mkdir html
	$(OCAMLDOC) $(OCAMLDOCFLAGS) -d html -I +camlp4 \
	  bitstring.mli bitstring_persistent.mli bitmatch.ml
endif

# Install.

install:
	ocamlfind install bitstring META *.mli *.cmx *.cma *.cmxa *.a *.so \
		bitstring.cmi \
		bitstring_persistent.cmi \
		pa_bitstring.cmo
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

# Standard rules.

.mli.cmi:
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) -c $<
.ml.cmo:
	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) $(OCAMLCPACKAGES) -c $<
.ml.cmx:
	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) $(OCAMLOPTPACKAGES) -c $<

depend: .depend
	@for d in $(SUBDIRS); do $(MAKE) -C $$d $@; done

.depend: bitstring.ml bitstring.mli \
	  bitstring_persistent.ml bitstring_persistent.mli \
	  bitstring_objinfo.ml \
	  create_test_pattern.ml
	rm -f .depend
	$(OCAMLFIND) ocamldep $(OCAMLCPACKAGES) \
	  bitstring.ml bitstring.mli \
	  bitstring_objinfo.ml >> $@
	$(OCAMLFIND) ocamldep $(OCAMLCPACKAGES) -pp camlp4of \
	  bitstring_persistent.ml bitstring_persistent.mli \
	  create_test_pattern.ml >> $@

ifeq ($(wildcard .depend),.depend)
include .depend
endif

# Distribution.

dist:
	$(MAKE) check-manifest
	rm -rf $(PACKAGE)-$(VERSION)
	mkdir $(PACKAGE)-$(VERSION)
	tar -cf - -T MANIFEST | tar -C $(PACKAGE)-$(VERSION) -xf -
	$(INSTALL) -m 0755 configure $(PACKAGE)-$(VERSION)/
	$(INSTALL) -m 0644 config.h.in $(PACKAGE)-$(VERSION)/
	svn log -v > $(PACKAGE)-$(VERSION)/CHANGES
	tar zcf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)
	rm -rf $(PACKAGE)-$(VERSION)
	ls -l $(PACKAGE)-$(VERSION).tar.gz

check-manifest:
	svn -R list | grep -v '/$$' | sort > .check-manifest
	sort MANIFEST > .orig-manifest; \
	diff -u .orig-manifest .check-manifest; rv=$$?; \
	rm -f .orig-manifest .check-manifest; \
	exit $$rv

.PHONY: depend dist check-manifest dpkg doc \
	print-examples print-tests examples test benchmarks

.SUFFIXES:      .cmo .cmi .cmx .ml .mli .mll
